<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
    </script>
</head>
<body>
<h3>登录</h3>
<br>
用户名:<input id="name" type="text">
<br>
密码:<input id="password" type="text">
<br>
<button id="sumbit" type="text">登录</button>
</body>

<script>

    var userId;

    $("#sumbit").click(function () {
        $.ajax({
            url: "json/login",
            type: "post",
            data: {
                name: $("#name").val(),
                password: $("#password").val(),
            },
            success: function (result) {
                if (result.result == false) {
                    alert("登录失败,请检查账户名密码")
                } else {
                    alert("登录成功");

                    checkCookie();



                    function setCookie(cname,cvalue,exdays){
                        var d = new Date();
                        d.setTime(d.getTime()+(exdays*24*60*60*1000));
                        var expires = "expires="+d.toGMTString();
                        document.cookie = cname+"="+cvalue+"; "+expires;
                    }




                    function checkCookie() {
                        var user = result.user.name;
                        userId = result.user.id;
                            if (user != "" && user != null) {
                                setCookie("name", user, 300);
                                setCookie("userId", userId, 300);
                            }

                            window.location.href = "http://localhost:8080/";

                    }



                }
            }

        })
    });
</script>

</html>